package model

import (
	"gorm.io/gorm"
	"smart-health-platforms/config"
	"time"
	"user_srv/user"
)

type MembershipBenefits struct {
	Id        int64          `gorm:"column:id;type:bigint;primaryKey;not null;" json:"id"`
	LevelId   uint64         `gorm:"column:level_id;type:bigint UNSIGNED;comment:会员id;not null;" json:"level_id"` // 会员id
	Benefits  string         `gorm:"column:benefits;type:varchar(255);comment:会员权益;not null;" json:"benefits"`  // 会员权益
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (m *MembershipBenefits) TableName() string {
	return "membership_benefits"
}
func (m *MembershipBenefits) MemberDisplay() (list []*user.MemberCenter, err error) {
	err = config.DB.Model(&MembershipBenefits{}).
		Select("membership_benefits.*,membership_levels.*").
		Joins("join membership_levels on membership_benefits.level_id =membership_levels.id").
		Find(&list).Error
	return
}

// 增加权益
func (m *MembershipBenefits) Create() error {
	return config.DB.Create(&m).Error
}

type Membership struct {
	Id          int64          `gorm:"column:id;type:bigint;primaryKey;not null;" json:"id"`
	Name        string         `gorm:"column:name;type:varchar(255);comment:会员名称;not null;" json:"name"`         // 会员名称
	Grade       int32          `gorm:"column:grade;type:int;comment:会员等级;not null;default:0;" json:"grade"`      // 会员等级
	Description string         `gorm:"column:description;type:text;comment:等级描述;" json:"description"`            // 等级描述
	Benefits    string         `gorm:"column:benefits;type:varchar(255);comment:会员权益;not null;" json:"benefits"` // 会员权益
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (m *MembershipBenefits) MemberDetails() (list Membership, err error) {
	err = config.DB.Model(&MembershipBenefits{}).
		Select("membership_benefits.*,membership_levels.*").
		Joins("join membership_levels on membership_benefits.level_id =membership_levels.id").
		Where("membership_benefits.level_id = ?", m.LevelId).Find(&list).Error
	return
}
